package copy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import copy.com.R;
import copy.pojo.Category;
import copy.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 *
 **/
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private CategoryService categoryService;

    /**
     * 复用
     * 查分类回填下拉框
     * 菜品分类数据列表查询
     * type=0查整张表数据 所以要对type参数做非空判断
     * 根据type查询list数据
     */
    @GetMapping("/list")
    public R<List<Category>> type(Category category) {
        String key="catetype_"+category.getType()+ "_8";
   String reresource  =  redisTemplate.opsForValue().get(key);
        if (reresource != null) {
           List<Category>resouce= JSON.parseObject(reresource, new TypeReference<List<Category>>() {
            });
            return R.success(resouce);
        }
//        List<Category> list = categoryService.list();
        LambdaQueryWrapper<Category> ca = new LambdaQueryWrapper<>();
        ca.eq(category.getType()!=null,Category::getType, category.getType());
        List<Category> list = categoryService.list(ca);
        redisTemplate.opsForValue().set(key, JSON.toJSONString(list), 30, TimeUnit.MINUTES);
        return R.success(list);
    }


    /**
     * 根据id修改分类数据
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        String keys = "Capage_1_12";
        if (keys != null) {
            redisTemplate.delete(keys);
        }

        return R.success("");
    }

    /**
     * 根据id删除分类
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long ids) {
//        String keys = "Capage_1_12";
        Set<String> keys = redisTemplate.keys("Capage_*");
        if (keys != null) {
            redisTemplate.delete(keys);
        }
        categoryService.removeWithDishSetmeal(ids);
        return R.success("");
    }

    /**
     * 新增分类
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
//        String pet = "Capage_*";
//        Set<String> keys = redisTemplate.keys("*"+"Capage_"+"*");
        String keys = "Capage_1_12";
        if (keys != null) {
            redisTemplate.delete(keys);
        }
        categoryService.save(category);
        return R.success("");
    }

    /**
     *
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        String key = "Capage_" + page + "_12";
      String s = redisTemplate.opsForValue().get(key);
        if (s != null) {
        Page<Category> Pagecaches =  JSON.parseObject(s,new TypeReference<Page<Category>>(){});
            return R.success(Pagecaches);
        }
        Page<Category> caPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> ca = new LambdaQueryWrapper<>();
        ca.orderByDesc(Category::getCreateTime);
        categoryService.page(caPage, ca);
        redisTemplate.opsForValue().set(key, JSON.toJSONString(caPage), 15, TimeUnit.MINUTES);
        return R.success(caPage);
    }

}
/*
 */
/**
 * 复用
 * 查分类回填下拉框
 * 菜品分类数据列表查询
 * type=1菜品
 *//*

//    @GetMapping("/list")
public R<List<Category>> savelist(Category category) {
    LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Category::getType, category.getType());
    List<Category> list = categoryService.list(wrapper);
    return R.success(list);
}


    /**
     * 根据id修改分类数据
//    @PutMapping

public R<String> update(@RequestBody Category category) {
    categoryService.updateById(category);
    return R.success("ok");
}


/**
     * 根据id删除分类
     *
//    @DeleteMapping
public R<String> deleteTwo(Long ids) {
    categoryService.removeWithDishSetmeal(ids);
    return R.success("ok");
}



    /**
     * 新增分类
     * @param category
     * @return

//    @PostMapping
public R<String> save(@RequestBody Category category) {
    categoryService.save(category);
    return R.success(null);
}




   /**
     *
     * @param page
     * @param pageSize
     * @return

//    @GetMapping("/page")
public R<Page> page(@RequestParam(required = false, defaultValue = "1") int page,
                    @RequestParam int pageSize) {
    Page<Category> categoryPage = new Page<>(page, pageSize);
    LambdaQueryWrapper<Category> la = new LambdaQueryWrapper<>();
    la.orderByDesc(Category::getCreateTime);
    categoryService.page(categoryPage,la);
    return R.success(categoryPage);
}
*/
